package com.base.datastructer.linklist;


import java.util.Iterator;
import java.util.Scanner;

import com.base.datastructer.ILinkList;
import com.base.datastucter.collection.LinkList;


public class SignleList implements LinkList{
    private NodeSingle pHead; // DAU
    private NodeSingle pTail; // CUOI
    
    public SignleList(){
        this.pHead = null;
        this.pTail = null;
    }
    
    public void addTail(NodeSingle node){
        if(node == null){
            return;
        }
        if(this.pHead == null && this.pTail == null){
            this.pHead = this.pTail = node;
        }else{
            node.pNext = this.pTail;
            this.pTail = null;
            this.pTail = node;
        }
    }
    
    public void addHead(NodeSingle node){
        if(node == null){
            return;
        }
        if(this.pHead == null && this.pTail == null){
            this.pHead = this.pTail = node;
        }else{
            this.pHead.pNext = node;
            this.pHead = node;
        }
    }
    
    public NodeSingle getTail(){
        NodeSingle nodeTail = null;
        if(this.pHead == this.pTail){
            nodeTail = this.pHead;
            this.pHead = null;
            this.pTail = null;
        }else{
            nodeTail = this.pHead;
            this.pTail = this.pHead.pNext;
            nodeTail.pNext = null;
        }
        return nodeTail;
    }
    
    public NodeSingle findNode(int x){
        NodeSingle temp = this.pTail;
        while(temp.pNext != null){
            if(temp.getData() == x){
                return temp;
            }
            temp = temp.pNext;
        }
        return null;
    }
    
    public void addAfterNode(int beforNodeData,int x){
        NodeSingle beforNode =  this.findNode(beforNodeData);
        if(beforNode == null){
            return;
        }
        NodeSingle nodeAdd = new NodeSingle();
        nodeAdd.setData(x);
        nodeAdd.pNext = beforNode.pNext;
        beforNode.pNext = nodeAdd;
    }
    
    public NodeSingle getNodeBefor(int value){
        NodeSingle temp = this.pTail;
        while(temp != null){
            if(temp.pNext.getData() == value){
                return temp;
            }
            temp = temp.pNext;
        }
        return null;
    }
    public NodeSingle getNode(int value){
        NodeSingle temp = null;
        temp = this.pTail;
        while(temp != null){
            if(temp.getData() == value){
                if(this.pTail.getData() == value){
                    //get tail
                    return  this.getTail();
                }
                if(this.pHead.getData() == value){
                    //get head
                    return this.getHead();
                }
                NodeSingle beforNode = this.getNodeBefor(temp.getData());
                beforNode.pNext = temp.pNext;
                temp.pNext = null;
                return temp;
            }
            temp = temp.pNext;
        }
        return null;
    }
  
    public void sortLinkList(){
       for(NodeSingle temp1 = this.pTail ; temp1.pNext != null ;temp1 = temp1.pNext){
           for(NodeSingle temp2 = temp1.pNext ; temp2 != null ;temp2 = temp2.pNext){
               if(temp1.getData() > temp2.getData()){
                   int temp = temp1.getData();
                   temp1.setData(temp2.getData());
                   temp2.setData(temp);
               }
           }
       }
    }
    public void printLinkList(){
        System.out.println("========================================\n");
        NodeSingle temp = null;
        temp = this.pTail;
        int i = 1;
        while(temp != null){
            System.out.print(temp.getData() + " " );
            temp = temp.pNext;
            i++;
        }
        System.out.println("========================================\n");
    }

    @Override
    public Iterator<ILinkList> iterator() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public NodeSingle getHead() {
        // TODO Auto-generated method stub
        NodeSingle nodeHead = null;
        if(this.pHead == this.pTail){
            nodeHead = this.pHead;
            this.pHead = null;
            this.pTail = null;
        }else{
            nodeHead = this.pHead;
            NodeSingle beforNode = this.getNodeBefor(nodeHead.getData());
            beforNode.pNext = null;
            this.pHead = beforNode;
        }
        return nodeHead;
    }

    public void removeLinkList() {
        // TODO Auto-generated method stub
        NodeSingle temp = this.pTail;
        while(temp != null){
            this.getHead();
            temp = this.pTail;
        }
    }

    public void inputLinkList() {
        // TODO Auto-generated method stub
        Scanner input =  new Scanner(System.in);
        System.out.println("Input number element signle list :");
        int numOfElement = input.nextInt();
        for(int i=0 ; i < numOfElement ;i++){
            NodeSingle inputNodeSignle = new NodeSingle();
            inputNodeSignle.inputSignleList();
            this.addTail(inputNodeSignle);
        }
    }

    @Override
    public void insertionSort() {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mergeSort() {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void quickSort() {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void bubbleSort() {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void heapSort() {
        // TODO Auto-generated method stub
        
    }


}
